<?php
  defined('ACC')||exit('ACC Denied'); 
  class usermodel extends model{
      protected $table = 'user';
      protected $pk = 'user_id';
      protected $fields = array('user_id', 'user_name', 'user_pwd', 'class_id', 'user_phone', 'user_email', 'user_category');
      protected $auto = array(
                              array('user_category', 'value', 'stu')
                              ); 
      
      
      public function reg($data) {
        if($data['user_pwd']) {
            $data['user_pwd'] = $this->encpwd($data['user_pwd']);
        }

        return $this->add($data);
      }
                              
      public function encpwd($pwd){
         return md5($pwd);      
      }
                              
      public function checkUser($username,$pwd='') {
        if($pwd == '') {
            $sql = 'select count(*) from ' . $this->table . " where user_name='" .$username . "'";
            return $this->db->getOne($sql);
        } else {
            $sql = "select user_id,user_name,user_pwd,class_id,user_phone,user_email,user_category from " . $this->table . " where user_name= '" . $username . "'";

            $row = $this->db->getRow($sql);

//            print_r($row);
            
            if(empty($row)) {
                return false;
            }

            if($row['user_pwd'] != $this->encpwd($pwd)) {
                return false;
            }

            unset($row['user_pwd']);
            return $row;
        }
    }
    
    public function numofcompany($userid){
       $sql = 'select count(*) from company where user_id = ' . $userid;
       return $this->db->getOne($sql); 
    }
        
            
  }
?>
